package net.chasing.retrofit;

import net.chasing.retrofit.bean.req.DownloadInfoReq;
import net.chasing.retrofit.callback.AbstractResponseCallback;
import net.chasing.retrofit.callback.ResponseBodyCallback;
import net.chasing.retrofit.engine.ApiV1Engine;
import net.chasing.retrofit.engine.FileEngine;

import io.reactivex.rxjava3.subscribers.ResourceSubscriber;
import okhttp3.ResponseBody;

/**
 * 请求管理类
 */
public class RequestManager {
    private static final RequestManager mRequestManager = new RequestManager();

    private ApiV1Engine mApiV1Engine;
    private FileEngine mFileEngine;


    private RequestManager() {

    }

    public static RequestManager getInstance() {
        return mRequestManager;
    }

    //更换IP及端口port时需要对应重新生成请求的Engine
    public void refresh() {

    }

    // region api/v1
    // 获取验证码
    public ResourceSubscriber<?> smscode(Object req, AbstractResponseCallback<Object> callback) {
        if (mApiV1Engine == null)
            mApiV1Engine = new ApiV1Engine();
        return mApiV1Engine.smscode(req, callback);
    }


    // endregion

    // region 文件下载
    public ResourceSubscriber<ResponseBody> downloadFile(String url, ResponseBodyCallback callback) {
        if (url == null)
            return null;
        if (callback == null)
            return null;
        if (mFileEngine == null)
            mFileEngine = new FileEngine();
        return mFileEngine.downloadFile(url, callback);
    }

    public ResourceSubscriber<ResponseBody> downloadFile(DownloadInfoReq infoReq, ResponseBodyCallback callback) {
        if (infoReq == null)
            return null;
        if (callback == null)
            return null;
        if (mFileEngine == null)
            mFileEngine = new FileEngine();
        return mFileEngine.downloadFile(infoReq, callback);
    }

    // endregion
}